package com.steam.sys.service.impl;

import com.steam.basic.service.impl.BaseServiceImpl;
import com.steam.sys.domain.Role;
import com.steam.sys.mapper.RoleMapper;
import com.steam.sys.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void save(Role role) {
        super.save(role);
        //添加当前角色的权限
        roleMapper.addRolePermission(role);
        //添加当前角色的菜单
        roleMapper.addRoleMenu(role);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        super.remove(id);
        //根据角色id删除当前角色的权限
        roleMapper.delPermissionByRoleId(id);
        //根据角色id删除当前角色的菜单
        roleMapper.delMenuByRoleId(id);
        //根据角色id删除当前员工的中间数据
        roleMapper.delEmployeeByRoleId(id);
    }

    @Override
    @Transactional
    public void update(Role role) {
        super.update(role);
        //根据角色id删除当前角色的权限
        roleMapper.delPermissionByRoleId(role.getId());
        //根据角色id删除当前角色的菜单
        roleMapper.delMenuByRoleId(role.getId());
        //添加当前角色的权限
        roleMapper.addRolePermission(role);
        //添加当前角色的菜单
        roleMapper.addRoleMenu(role);
    }
}
